package com.atguigu.jxc.service.impl;

import com.atguigu.jxc.dao.GoodsTypeDao;
import com.atguigu.jxc.domain.ErrorCode;
import com.atguigu.jxc.domain.ServiceVO;
import com.atguigu.jxc.domain.SuccessCode;
import com.atguigu.jxc.entity.Goods;
import com.atguigu.jxc.entity.GoodsType;
import com.atguigu.jxc.entity.Unit;
import com.atguigu.jxc.service.GoodsTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author 里
 * @Description:
 */
@Service
public class GoodsTypeServiceImpl implements GoodsTypeService {

    @Autowired
    private GoodsTypeDao goodsTypeDao;

    @Override
    public String getGoodsType() {
        return goodsTypeDao.getGoodsType();
    }

    @Override
    public Map<String, Object> getGoodsUnitList() {
        Map map = new HashMap();
        List<Unit> unitList =  goodsTypeDao.getGoodsUnitList();
        if (!CollectionUtils.isEmpty(unitList)){
            for (Unit unit : unitList) {
                map.put("unitId",unit.getUnitId());
                map.put("unitName",unit.getUnitName());
            }
        }
        return map;
    }

    @Override
    public Map<String, Object> getGoodsList(Integer page, Integer rows, String goodsName, Integer goodsTypeId) {
        Map map = new HashMap();
        List<Goods> goodsList =  goodsTypeDao.getGoodsList(page, rows, goodsName, goodsTypeId);
        if (!CollectionUtils.isEmpty(goodsList)){
            for (Goods goods : goodsList) {
                map.put("goodsId",goods.getGoodsId());
                map.put("goodsCode",goods.getGoodsCode());
                map.put("goodsName",goods.getGoodsName());
                map.put("inventoryQuantity",goods.getInventoryQuantity());
                map.put("lastPurchasingPrice",goods.getLastPurchasingPrice());
                map.put("minNum",goods.getMinNum());
                map.put("goodsModel",goods.getGoodsModel());
                map.put("goodsProducer",goods.getGoodsProducer());
                map.put("purchasingPrice",goods.getPurchasingPrice());
                map.put("remarks",goods.getRemarks());
                map.put("sellingPrice",goods.getSellingPrice());
                map.put("state",goods.getState());
                map.put("goodsUnit",goods.getGoodsUnit());
                map.put("goodsTypeId",goods.getGoodsTypeId());
                map.put("goodsTypeName",goods.getGoodsCode());
                map.put("saleTotal",goods.getGoodsCode());
            }
        }
        return map;
    }

    @Override
    public void saveOrUpdateGoods(Goods goods) {
        if (goods.getGoodsId() != null) {
            goods =  goodsTypeDao.getGoodsById(goods.getGoodsId());
            goodsTypeDao.updateGoodsById(goods);
        }else {
            goodsTypeDao.addGoods(goods);
        }
    }

    @Override
    public void saveGoodsType(String goodsTypeName, Integer pId) {
         goodsTypeDao.saveGoodsType(goodsTypeName,pId);
    }

    @Override
    @Transactional
    public void deleteGoodsById(Integer goodsId) {
        Goods goods = goodsTypeDao.getGoodsById(goodsId);
        // 判断商品状态, 入库、有进货和销售单据的不能删除
        // 0 初始化状态 1 期初库存入仓库  2  有进货或者销售单据
        if (goods.getState() == 0) {
            goodsTypeDao.deleteGoods(goodsId);
        }else {
            return;
        }
    }

    @Override
    public void deleteGoodsType(Integer goodsTypeId) {
        goodsTypeDao.deleteGoodsType(goodsTypeId);
    }

    @Override
    public Map<String, Object> getNoInventoryQuantity(Integer page, Integer rows, String nameOrCode) {
        Map map = new HashMap();
        String name= null;
        String code = null;
        if(!StringUtils.isEmpty(nameOrCode)){
            if(nameOrCode.matches("^[0-9]*$")){
                code=nameOrCode;
            }else {
                name=nameOrCode;
            }
        }
        List<Goods> goodsList = goodsTypeDao.getNoInventoryQuantity(page,rows,name,code);
        List<Goods> goodsList1 = new ArrayList<>();
        if(!CollectionUtils.isEmpty(goodsList)){
            for (Goods goods : goodsList) {
                GoodsType goodsType = goodsTypeDao.getGoodsTypeById(goods.getGoodsTypeId());
                if(goodsType!=null){
                    goods.setGoodsTypeName(goodsType.getGoodsTypeName());
                }
                goodsList1.add(goods);
            }
        }
        //获取无库存的商品总数量
        map.put("total",goodsTypeDao.getNoInventoryQuantityCount());
        map.put("rows",goodsList1);
        return map;
    }

    @Override
    public Map<String, Object> getHasInventoryQuantity(Integer page, Integer rows, String nameOrCode) {
        Map<String,Object> map = new HashMap<>();
        String name= null;
        String code = null;
        if(!StringUtils.isEmpty(nameOrCode)){
            if(nameOrCode.matches("^[0-9]*$")){
                code=nameOrCode;
            }else {
                name=nameOrCode;
            }
        }
        List<Goods> goodsList = goodsTypeDao.getHasInventoryQuantity(page,rows,name,code);
        List<Goods> goodsList1 = new ArrayList<>();
        if(!CollectionUtils.isEmpty(goodsList)){
            for (Goods goods : goodsList) {
                GoodsType goodsType = goodsTypeDao.getGoodsTypeById(goods.getGoodsTypeId());
                if(goodsType!=null){
                    goods.setGoodsTypeName(goodsType.getGoodsTypeName());
                }
                goodsList1.add(goods);
            }
        }
        //获取有库存的商品总数量
        map.put("total",goodsTypeDao.getHasInventoryQuantityCount());
        map.put("rows",goodsList1);
        return map;
    }

    @Override
    public ServiceVO saveStock(Integer goodsId, Integer inventoryQuantity, double purchasingPrice) {
        Goods goods = new Goods();
        goods.setGoodsId(goodsId);
        goods.setInventoryQuantity(inventoryQuantity);
        goods.setPurchasingPrice(purchasingPrice);
        goodsTypeDao.updateGoodsById(goods);
        return new ServiceVO(SuccessCode.SUCCESS_CODE,SuccessCode.SUCCESS_MESS);
    }

    @Override
    public ServiceVO deleteStock(Integer goodsId) {
        //先进行查询
        Goods goodsById = goodsTypeDao.getGoodsById(goodsId);
        if(goodsById!=null){
            Integer state = goodsById.getState();
            if(state == 1){
                //表示商品已经入库 不能进行删除
                return new ServiceVO(ErrorCode.STORED_ERROR_CODE,ErrorCode.STORED_ERROR_MESS);
            }else if(state == 2) {
                //表示商品有进货单据  不能进行删除
                return new ServiceVO(ErrorCode.HAS_FORM_ERROR_CODE, ErrorCode.HAS_FORM_ERROR_MESS);
            }else {
                Goods goods = new Goods();
                goods.setGoodsId(goodsId);
                goods.setInventoryQuantity(0);
                goodsTypeDao.updateGoodsById(goods);
                return new ServiceVO(SuccessCode.SUCCESS_CODE,SuccessCode.SUCCESS_MESS);
            }
        }else {
            return new ServiceVO(ErrorCode.NULL_POINTER_CODE,ErrorCode.NULL_POINTER_MESS);
        }
    }

    @Override
    public Map<String, Object> listAlarm() {
        Map<String,Object> map = new HashMap<>();
        //查询全部
        List<Goods> goodsList = goodsTypeDao.getAllGoodsList();
        if(!CollectionUtils.isEmpty(goodsList)){
            List<Goods> collect = goodsList.stream().filter(goods -> {
                return goods.getInventoryQuantity() <= goods.getMinNum();
            }).collect(Collectors.toList());
            List<Goods> listAlarmGoods = new ArrayList<>();
            if(!CollectionUtils.isEmpty(collect)){
                for (Goods goods : collect) {
                    GoodsType goodsType = goodsTypeDao.getGoodsTypeById(goods.getGoodsTypeId());
                    if(goodsType!=null){
                        goods.setGoodsTypeName(goodsType.getGoodsTypeName());
                    }
                    listAlarmGoods.add(goods);
                }
            }
            map.put("rows",listAlarmGoods);
        }
        return map;
    }
}
